<template>
  <!-- 放置一个图标 -->
  <div>
    <svg-icon :icon-class="isFullscreen ? 'exit-fullscreen' : 'fullscreen'" class="fullscreen" @click="toggleScreen" />
  </div>
</template>

<script>
import screenfull from 'screenfull'
export default {
  name: 'ScreenFull',
  data() {
    return {
      isFullscreen: false
    }
  },
  mounted() {
    // 当我们按住ESC键,退出全屏以后呢,图标并没有进行切换
    // 原因是因为我们没有监听有没有退出全屏
    // 怎么进行监听? 使用screenfull提供的chang方法
    screenfull.on('change', () => {
      // screenfull 提供了一个变量,状态 isFullscreen 代表当前是否是全屏
      //  isFullscreen = true 说明是全屏  === false 说明是非全屏
      this.isFullscreen = screenfull.isFullscreen
    })
  },
  methods: {
    toggleScreen() {
      // 当点击全屏图标时,需要将isFullscreen 设置为true
      this.isFullscreen = true
      // 调用screenfull 插件提供的 toggle 方法, 实现切换全屏
      screenfull.toggle()
    }
  }
}

</script>

<style lang="scss" scoped>

.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>
